package org.jeecg.modules.demo.dz.service.impl;

import org.jeecg.modules.demo.dz.entity.DzSupplier;
import org.jeecg.modules.demo.dz.entity.DzSupplierPhone;
import org.jeecg.modules.demo.dz.mapper.DzSupplierPhoneMapper;
import org.jeecg.modules.demo.dz.mapper.DzSupplierMapper;
import org.jeecg.modules.demo.dz.service.IDzSupplierService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 供应链管理
 * @Author: jack
 * @Date:   2024-11-24
 * @Version: V1.0
 */
@Service
public class DzSupplierServiceImpl extends ServiceImpl<DzSupplierMapper, DzSupplier> implements IDzSupplierService {

	@Autowired
	private DzSupplierMapper dzSupplierMapper;
	@Autowired
	private DzSupplierPhoneMapper dzSupplierPhoneMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(DzSupplier dzSupplier, List<DzSupplierPhone> dzSupplierPhoneList) {
		dzSupplierMapper.insert(dzSupplier);
		if(dzSupplierPhoneList!=null && dzSupplierPhoneList.size()>0) {
			for(DzSupplierPhone entity:dzSupplierPhoneList) {
				//外键设置
				entity.setSupplierId(dzSupplier.getId());
				dzSupplierPhoneMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(DzSupplier dzSupplier,List<DzSupplierPhone> dzSupplierPhoneList) {
		dzSupplierMapper.updateById(dzSupplier);
		
		//1.先删除子表数据
		dzSupplierPhoneMapper.deleteByMainId(dzSupplier.getId());
		
		//2.子表数据重新插入
		if(dzSupplierPhoneList!=null && dzSupplierPhoneList.size()>0) {
			for(DzSupplierPhone entity:dzSupplierPhoneList) {
				//外键设置
				entity.setSupplierId(dzSupplier.getId());
				dzSupplierPhoneMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		dzSupplierPhoneMapper.deleteByMainId(id);
		dzSupplierMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			dzSupplierPhoneMapper.deleteByMainId(id.toString());
			dzSupplierMapper.deleteById(id);
		}
	}
	
}
